<%--
  Created by IntelliJ IDEA.
  User: 74018
  Date: 2019-01-10
  Time: 21:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>服务</title>
    <jsp:include page="${pageContext.request.contextPath}/common/common.jsp"/>
</head>
<body>
<div class="container">
    <div class="container">
        <table id="tb"></table>
    </div>
</div>
<form action="#" id="myTool" class="form-inline">
    <div class="form-group">
        <input type="button" class="btn btn-primary" onclick="toadd();" value="添加">
    </div>
    <div class="form-group">
        <input type="button" class="btn btn-primary" onclick="dodel();" value="删除">
    </div>
</form>

<!-- 添加服务 -->
<div class="modal fade" id="modelAdd" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="modelTitleId">添加服务</h4>
            </div>
            <div class="modal-body">
                <form id="addFrom" action="#" class="form-horizontal">
                    <div class="form-group">
                        <label for="name" class="col-sm-2">名称:</label>
                        <div class="col-sm-10">
                            <input id="name" class="form-control" name="name" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="price" class="col-sm-2">价格:</label>
                        <div class="col-sm-10">
                            <input id="price" class="form-control" name="price" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="note" class="col-sm-2">备注:</label>
                        <div class="col-sm-10">
                            <input id="note" class="form-control" name="note" />
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="doAdd();">添加</button>
            </div>
        </div>
    </div>
</div>

<!-- 修改服务 -->
<div class="modal fade" id="modelUp" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="modelTitleId2">添加服务</h4>
            </div>
            <div class="modal-body">
                <form id="upForm" action="#" class="form-horizontal">
                    <div class="form-group">
                        <input name="id" type="hidden" />
                        <label for="nameu" class="col-sm-2">名称:</label>
                        <div class="col-sm-10">
                            <input id="nameu" class="form-control" name="name" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="priceu" class="col-sm-2">价格:</label>
                        <div class="col-sm-10">
                            <input id="priceu" class="form-control" name="price" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="noteu" class="col-sm-2">备注:</label>
                        <div class="col-sm-10">
                            <input id="noteu" class="form-control" name="note" />
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="doUp();">添加</button>
            </div>
        </div>
    </div>
</div>

</body>
</html>
<script>
    $(function () {
        $("#tb").bootstrapTable({
            url:"${pageContext.request.contextPath}/repairOrder/query",
            toolbar:'#myTool',//工具按钮用哪个容器
            striped: true,//隔行换色
            //showRefresh: true,//是否显示刷新按钮
            clickToSelect: true,
            height: 500,//行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "id",//每一行的唯一标识，一般为主键列
            //showToggle:true,//是否显示详细视图和列表视图的切换按钮
            pagination:true, //是否显示分页（*）
            pageSize : 10,//单页记录数
            pageList : [ 10, 20, 30 ],//可选择单页记录数
            columns:[{
                checkbox:true,
            },{
                field:'id',
                title:'编号'
            },{
                field:'name',
                title:'名称'
            },{
                field:'price',
                title:'价格'
            },{
                field:'note',
                title:'备注信息'
            },{title:'操作',formatter:function (value,row,index){
                    return "<button class='btn  btn-info' onclick='toUpdate("+row.id+")'>修改</button>";
                }
            }]
        });
    })
    // 添加
    function toadd() {
        $("#modelAdd").modal("show");
        $("#addFrom input[type='text']").val("");
    }
    function doAdd() {
        var formStr = $("#addFrom").serialize();
        var data = decodeURIComponent(formStr,true);
        $.ajax({
            url:"${pageContext.request.contextPath}/repairOrder/addService",
            data:data,
            type:'post',
            success:function (data) {
                if (data>0){
                    swal("提示信息", "添加成功!", "success");
                }else{
                    swal("提示信息", "出错了!", "error");
                }
                $("#modelAdd").modal("hide");
                loadTb();
            }
        });
    }
    function upAdd() {
        var formStr = $("#addFrom").serialize();
        var data = decodeURIComponent(formStr,true);
        $.ajax({
            url:"${pageContext.request.contextPath}/repairOrder/addService",
            data:data,
            type:'post',
            success:function (data) {
                if (data>0){
                    swal("提示信息", "添加成功!", "success");
                }else{
                    swal("提示信息", "出错了!", "error");
                }
                loadTb();
            }
        });
    }
    function toUpdate(id) {
        $("#modelUp").modal("show");
        var data = $("#tb").bootstrapTable('getRowByUniqueId', id);
        $("#upForm [name='id']").val(data.id);
        $("#upForm [name='name']").val(data.name);
        $("#upForm [name='price']").val(data.price);
        $("#upForm [name='note']").val(data.note);
    }
    function doUp() {
        var formStr = $("#upForm").serialize();
        var data = decodeURIComponent(formStr,true);
        $.ajax({
            url:"${pageContext.request.contextPath}/repairOrder/upService",
            data:data,
            type:'post',
            success:function (data) {
                if (data>0){
                    swal("提示信息", "添加成功!", "success");
                }else{
                    swal("提示信息", "出错了!", "error");
                }
                $("#modelUp").modal("hide");
                loadTb();
            }
        });
    }
    function dodel() {
        var rows = $("#tb").bootstrapTable("getSelections") ;
        if (rows.length==0){
            swal("提示信息", "请选择要删除的数据!", "error");
        }
        else{
            var ids = '';
            $.each(rows,function (index,row) {
                ids = ids + row.id + "-" ;
            }) ;
            swal({
                title: "您确定要删除这条信息吗",
                text: "删除后将无法恢复，请谨慎操作！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "删除",
                closeOnConfirm: false
            }, function () {
                $.post("${pageContext.request.contextPath}/repairOrder/delService",{'idStr': ids},function(data){
                    if (data>0){
                        swal("提示信息", "添加成功!", "success");
                    }else{
                        swal("提示信息", "出错了!", "error");
                    }
                    loadTb();
                });
            })
        }
    }
    function loadTb() {
        $.post("${pageContext.request.contextPath}/repairOrder/query",function (data) {
            $("#tb").bootstrapTable("load",data);
        });
    }
</script>
